/**
 * Created by gastrodia on 14-9-20.
 */

var crypto = require('crypto');

var AESCrypt = {};

AESCrypt.decrypt = function(cryptkey, iv, encryptdata) {
    encryptdata = new Buffer(encryptdata, 'base64').toString('binary');
    var decipher = crypto.createDecipheriv('aes-128-cbc', cryptkey, iv);
    var decoded  = decipher.update(encryptdata, 'binary', 'utf8');

    decoded += decipher.final('utf8');
    return decoded;
}

AESCrypt.encrypt = function(cryptkey, iv, cleardata) {
    var encipher = crypto.createCipheriv('aes-128-cbc', cryptkey, iv);
    var encryptdata  =   encipher.update(cleardata, 'utf8', 'binary');
    encryptdata += encipher.final('binary');
    var encode_encryptdata = new Buffer(encryptdata, 'binary').toString('base64');
    return encode_encryptdata;
}

module.exports = AESCrypt;